package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class MyMetaObjectMapper implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {

        long id = Thread.currentThread().getId();
        log.info("线程"+id);

        log.info("公共填充[inster...]");
        //两种自动装配方式
//        metaObject.setValue("createTime", LocalDateTime.now());
////        strictInsertFill(metaObject,"createTime",LocalDateTime.class,LocalDateTime.now());
//
//        metaObject.setValue("updateTime",LocalDateTime.now());
//
//        metaObject.setValue("createUser",BaseContext.getContext());
//        metaObject.setValue("updateUser",BaseContext.getContext());

        strictInsertFill(metaObject,"createTime",LocalDateTime.class,LocalDateTime.now());
        strictInsertFill(metaObject,"updateTime",LocalDateTime.class,LocalDateTime.now());

        strictInsertFill(metaObject,"createUser",Long.class,BaseContext.getContext());
        strictInsertFill(metaObject,"updateUser",Long.class,BaseContext.getContext());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共填充[update....]");
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("updateUser",BaseContext.getContext());
    }
}
